<?php
/**
 * Created by PhpStorm.
 * User: yuyiwen
 * Date: 2018/10/10
 * Time: 上午2:57
 */

namespace app\admin\model;


use think\Model;

class ProductGuige extends Model
{
    public function getProductGuige ($id)
    {
        # 根据产品id 查询规格表
        $guige = db('ProductGuige')->where('product_id' , $id)->select();

        # 根据规格id 获取 所有规格属性
        foreach ($guige as $key1 => $value1) {
            $guige[$key1]['attr'] = db('ProductGuigeAttr')->where('guige_id' , $value1['guige_id'])->select();
        }


        # 重组数组 （最多三级规格）
        $tmp_guige = $guige;
        foreach ($tmp_guige as $key1 => $value1) {
                foreach ($tmp_guige as $key2 => $value2) {
                    if ($value1['guige_id']  == $value2['pid'] && $value1['pid'] == 0) {
                        $guige[$key1]['son'][] = $value2;
                        unset($guige[$key2]);
                    }
                }
            }




            $tmp_guige = $guige;
            foreach ($tmp_guige as $key1 => $value1) {
                if (isset($value1['son'])) {
                    foreach ($value1['son'] as $k2 => $v2) {
                        foreach ($tmp_guige as $k3 => $v3) {

                            if ($v2['guige_id'] == $v3['pid']) {

                                $guige[$key1]['son'][$k2]['son'][] = $v3;
                                unset($guige[$k3]);
                            }
                        }
                    }
                }
            }

            return $guige;

    }
}